/**
 * \file
 *
 * Create a blank filesystem
 */
#include "precompiled.hpp"
#include "kfs_mkfs.hpp"
#include <boost/program_options.hpp>
#include <boost/program_options/cmdline.hpp>
//#include <boost/program_options/config.hpp>
//#include <boost/program_options/environment_iterator.hpp>
//#include <boost/program_options/eof_iterator.hpp>
#include <boost/program_options/errors.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/positional_options.hpp>
//#include <boost/program_options/value_semantic.hpp>
#include <boost/program_options/variables_map.hpp>
//#include <boost/program_options/version.hpp>
#include <cstdio>
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <iostream>

using std::cerr;  using std::cin;  using std::cout;  using std::endl;
//using std::ios;  using std::istringstream;
//using std::map;  using std::set;
using std::string;  using std::vector;

namespace po = boost::program_options;


int main(int argc, char *argv[])
{
    // Declare the supported options.
    po::options_description desc("Options for creating the filesystem");
    desc.add_options()
        ("block-size", po::value<unsigned>()->default_value(512),
            "Block size in bytes, power of two")
        ("num-blocks", po::value<unsigned>(), "Number of blocks")
        ("device", po::value<string>()->required(), "The device on which "
            "the filesystem will be created")
        ("help", "Produce this help message")
    ;
    po::positional_options_description pd;
    pd.add("device", 1);

    try
    {
        po::parsed_options parsed = po::command_line_parser(argc, argv)
        .options(desc).positional(pd).run();

        po::variables_map vm;
        po::store(parsed, vm);
        // Check for a "help" request before we enforce mandatory options.
        if (vm.count("help"))
        {
            cout << desc;
            return 0;
        }
        po::notify(vm);

        kfs_mkfs(vm);
        printf("File system initialized\n");
        return 0;
    }
    catch(po::error& e)
    {
        cerr << e.what() << endl;
        return 1;
    }
    catch(std::exception& e)
    {
        cerr << e.what() << endl;
        //fprintf(stderr, "%s: Unexpected exception: %s\n", __FUNCTION__,
        //    e.what());
        return 1;
    }
}
